package com.lanou.renrenxin.tools;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.android.volley.toolbox.ImageLoader;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by zouhui on 16/3/8.
 */
public class RenrenCache implements ImageLoader.ImageCache {
    private MemoryCache memoryCache;
    private DiskCache fileCache;

    public RenrenCache() {
        memoryCache = new MemoryCache();
        fileCache = new DiskCache();
    }

    @Override
    public Bitmap getBitmap(String url) {
        Bitmap bitmap = memoryCache.getBitmap(url);
        if (null == bitmap) {
            return fileCache.getBitmap(url);
        }
        return bitmap;
    }

    @Override
    public void putBitmap(String url, Bitmap bitmap) {
        Bitmap changedBitmap = decodeSampledBitmapFromResource(bitmap, 250, 100);
        memoryCache.putBitmap(url,bitmap);
        fileCache.putBitmap(url,bitmap);
    }

    public int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

    public Bitmap decodeSampledBitmapFromResource(Bitmap bitmap, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
//        BitmapFactory.decodeResource(res, resId, options);
        try {
            BitmapFactory.decodeStream(changeBitmap(bitmap), null, options);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        try {
            return BitmapFactory.decodeStream(changeBitmap(bitmap), null, options);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private InputStream changeBitmap(Bitmap bmp) throws IOException {
        ByteArrayOutputStream output = new ByteArrayOutputStream();//初始化一个流对象
        ByteArrayInputStream inputStream = null;
        try {
            bmp.compress(Bitmap.CompressFormat.PNG, 100, output);//把bitmap100%高质量压缩 到 output对象里
//            bmp.recycle();//自由选择是否进行回收
            byte[] result = output.toByteArray();
            inputStream = new ByteArrayInputStream(result);
            output.close();
//            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
        }
        return inputStream;
    }
}
